package com.papper.mng.controller.base;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.papper.common.bean.base.BaseConstFile;
import com.papper.common.constant.SystemConstants;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.DwzPage;
import com.papper.common.service.base.BaseConstFileService;
import com.papper.common.utils.tool.StringTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:文件资源
 */
@Controller
@RequestMapping("/base/const/file")
public class BaseConstFileController extends BaseAction {

    @Autowired
    BaseConstFileService baseConstFileService;

    /**
     * 文件资源
     *
     * @return
     */
    @RequestMapping(value = "")
    public String execute(HttpServletRequest _request, Model model) {
        // 字段查询条件
        Map<String, Object> objects = Maps.newHashMap();
        extraParams(objects, _request, BaseConstFile.class);
        modelParm(objects, model);
        // 分页查询条件
        int pageNum    = StringTool.parseInteger(_request.getParameter("pageNum"), 1);
        int numPerPage = StringTool.parseInteger(_request.getParameter("numPerPage"), SystemConstants.PAGE_LIMIT_16);
        // 数据查询组装
        Example                 example = buildExample(objects, model, BaseConstFile.class);
        PageInfo<BaseConstFile> pageRtn = baseConstFileService.findPage(pageNum, numPerPage, example);
        model.addAttribute("page", new DwzPage(pageNum, numPerPage, pageRtn.getTotal()));
        model.addAttribute("records", pageRtn.getList());
        return render("base/const/file/index");
    }

    /**
     * 文件资源_新增
     */
    @GetMapping(value = "/add")
    public String add_get() {
        return render("base/const/file/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, BaseConstFile baseConstFile) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }

        // 添加文件资源
        try {
            baseConstFile.setDataStatus(DataStatusEnum.OPEN.getCode());
            baseConstFile.setCreateTime(new Date());
            baseConstFile.setModifyTime(new Date());
            baseConstFileService.save(baseConstFile);
            return new DwzResponse("200", "文件资源添加成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "文件资源添加失败:" + e.getMessage() + "!!!");
        }
    }

    /**
     * 文件资源_更新
     */
    @GetMapping(value = "/edit")
    public String edti_get(Model model, Long id) {
        BaseConstFile entity = baseConstFileService.findById(id);
        model.addAttribute("entity", entity);
        return render("base/const/file/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, BaseConstFile baseConstFile) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (baseConstFile == null || baseConstFile.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            baseConstFile.setModifyTime(new Date());
            baseConstFileService.update(baseConstFile);
            return new DwzResponse("200", "文件资源修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "文件资源修改失败:" + e.getMessage());
        }
    }

    /**
     * 文件资源_删除
     *
     * @param id
     * @
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del_post(HttpServletRequest _request, Long id) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || id.intValue() <= 0) {
            return new DwzResponse("300", "参数ID为空！");
        }

        BaseConstFile old = baseConstFileService.findById(id);

        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        try {
            baseConstFileService.deleteById(id);
            return new DwzResponse("200", "文件资源删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "文件资源删除失败:" + e.getMessage());
        }
    }

    /**
     * 文件资源_详情
     *
     * @return
     */
    @GetMapping(value = "/detail")
    public String detail(HttpServletResponse _response, Model model, Long id) {
        BaseConstFile baseConstFile = baseConstFileService.findById(id);
        model.addAttribute("entity", baseConstFile);
        return render("base/const/file/detail");
    }

}



